package com.youlu.campus.service.feed.impl;

import com.alibaba.fastjson.JSON;

import com.youlu.campus.entity.ActivityFeedInfo;
import com.youlu.campus.service.feed.ActivityFeedInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class ActivityFeedInfoServiceImpl implements ActivityFeedInfoService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    /**
     * 活动Feed流-缓存前缀
     */
    public static final String FEED_PEFIX = "youlu:runtime:feed:activityId:%s:activityStatus:%d::page:%d";

    /**
     * 活动Feed流-缓存前缀，新前缀
     */
    public static final String FEED_CACHE_PEFIX = "youlu:runtime:feed:new.new:activityId:%s::page:%d";
    /**
     * 活动Feed流-缓存前缀，新前缀-List
     */
    public static final String FEED_LIST_CACHE_PEFIX = "youlu:runtime:feed:list:activityId:%s:activityStatus:%d";

    @Override
    public List<ActivityFeedInfo> getFeedInfoCache(String activityId, Integer activityStatus, Integer page) {
        log.info(":>>> 开始获取活动Feed缓存:{},{},{}", activityId, page);
        String key = String.format(FEED_PEFIX, activityId, activityStatus, page);
        Object data = redisTemplate.opsForValue().get(key);
        if (Objects.isNull(data)) {
            log.error(":>>> activityId:{},page:{} ,缓存数据不存在", activityId, page);
            return null;
        }
        List<ActivityFeedInfo> datas = JSON.parseArray((String) data, ActivityFeedInfo.class);
        log.info(":>>> activityId:{},page:{} ,缓存:{}", activityId, page, JSON.toJSONString(datas));
        return datas;
    }

    @Async
    @Override
    public void setFeedInfoCache(String activityId, Integer activityStatus, Integer page, List<ActivityFeedInfo> datas) {
        log.info(":>>> 开始设置活动Feed缓存:{},{},{}", activityId, page, JSON.toJSONString(datas));
        String key = String.format(FEED_PEFIX, activityId, activityStatus, page);
        redisTemplate.opsForValue().set(key, JSON.toJSONString(datas), 1, TimeUnit.HOURS);
        log.info(":>>> 设置活动Feed缓存缓存:{},{},{},一个小时后过期", activityId, page);
    }

    @Override
    public List<ActivityFeedInfo> getFeedInfo(String activityId, Integer page) {
        String key = String.format(FEED_CACHE_PEFIX, activityId, page);
        return null;
    }

    @Async
    @Override
    public void addActivityFeedInfo(String activityId, Integer activityStatus, ActivityFeedInfo req) {
        if (Objects.isNull(req) || StringUtils.isBlank(activityId) || Objects.isNull(activityStatus)) {
            return;
        }
        String key = String.format(FEED_LIST_CACHE_PEFIX, activityId, activityStatus);
        Long size = redisTemplate.opsForList().size(key);
        log.info(":>>> 添加Feed之前特别注意Feed流长度大小:{}", size);
        if (Objects.isNull(size)) {
            redisTemplate.opsForList().leftPush(key, JSON.toJSONString(req));
            return;
        }
        if (size.compareTo(50L) == -1) {
            redisTemplate.opsForList().rightPush(key, JSON.toJSONString(req));
            log.info(":>>> 添加Feed之后<50,特别注意Feed流长度大小:{}", redisTemplate.opsForList().size(key));
            return;
        }
        redisTemplate.opsForList().rightPop(key);
        redisTemplate.opsForList().leftPush(key, JSON.toJSONString(req));
        log.info(":>>> 添加Feed之后>50,特别注意Feed流长度大小:{}", redisTemplate.opsForList().size(key));
    }

    @Override
    public void removeActivityFeedInfo(String activityId, Integer activityStatus, ActivityFeedInfo req) {

    }

    @Override
    public List<ActivityFeedInfo> getFeedInfo(String activityId, Integer activityStatus, Integer page) {
        String key = String.format(FEED_LIST_CACHE_PEFIX, activityId, activityStatus);
        try {
            //需求HPROJ-1792 编号跑马灯数据比较少
            List<String> s = redisTemplate.opsForList().range(key, 0, 49);
            if (Objects.isNull(s)) {
                return new ArrayList<>();
            }
            List<ActivityFeedInfo> data = new ArrayList<>();
            for (String ss : s) {
                ActivityFeedInfo a = JSON.parseObject(ss, ActivityFeedInfo.class);
                if (Objects.nonNull(a)) {
                    data.add(a);
                }
            }
            log.info(":>>> 活动:{},类型:{}, Feed流数据大小:{}", activityId, activityStatus, data == null ? 0 : data.size());
            return data;
        } catch (Exception e) {
            log.error(":>>> 获取Feed流错误:{}", e);
            return new ArrayList<>();
        }

    }
}
